package com.xueyu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xueyu.dao.DetailsDao;
import com.xueyu.dao.OrdersDao;
//import com.xueyu.dao.OrderdetailDao;
import com.xueyu.domain.Orderdetail;
import com.xueyu.domain.goods.Details;
//import com.xueyu.domain.goods.Orderdetail;
import com.xueyu.domain.goods.Orders;
import com.xueyu.domain.goods.Product;
import com.xueyu.util.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/Cords")
public class CordsContoller {
//    @Autowired
//    OrderdetailDao OrderdetailDao;
    @Autowired
    OrdersDao OrdersDao;

    @Autowired
    DetailsDao DetailsDao;
    @RequestMapping("/list")
    public ResultBean getOrderDetailsByOrderNoList(@RequestBody Map map) {
        ResultBean resultBean = new ResultBean();
        int current = (Integer)map.get("Current");
        int size = (Integer)map.get("size");
        String orderno =(String) map.get("orderno");
        String style =(String) map.get("style");
        Page<Product> page = new Page<>(current, size);
        List<String> orderNoList = OrdersDao.getOrderNoList(page,orderno,style);
        String orderNos = "'" + String.join("','", orderNoList) + "'";
        List<Orders> orderDetails = OrdersDao.selectOrderDetailWithDrugsByOrderNoList(orderNos);
        resultBean.put("page",page);
        resultBean.put("list",orderDetails);
        return resultBean;
    }



    @RequestMapping("/list2")
    public ResultBean getOrderDetailsByOrderNoList2(@RequestBody Map map) {
        ResultBean resultBean = new ResultBean();
        int current = (Integer)map.get("Current");
        int size = (Integer)map.get("size");
        String orderno =(String) map.get("orderno");
        String style =(String) map.get("style");
        Page<Product> page = new Page<>(current, size);
        List<String> orderNoList = OrdersDao.getOrderNoList2(page,orderno,style);
        String orderNos = "'" + String.join("','", orderNoList) + "'";
        List<Orderdetail> orderDetails = OrdersDao.selectOrderDetailWithDrugsByOrderNoList2(orderNos);
        resultBean.put("page",page);
        resultBean.put("list",orderDetails);
        return resultBean;
    }







    @RequestMapping("/getid/{orderNo}")
    public ResultBean getid(@PathVariable("orderNo") String orderNo) {
        OrdersDao.updateOrderNo(orderNo); // 修改状态
        ResultBean resultBean = new ResultBean();
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String currentTime = sdf.format(date);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.YEAR, 1);
        Date oneYearLater = calendar.getTime();
        String currentTimes = sdf.format(oneYearLater);
        if (orderNo != null) {
            QueryWrapper<Orders> queryWrapper = Wrappers.query();
            queryWrapper.eq("orderNo", orderNo);
            List<Orders> drugList = OrdersDao.selectList(queryWrapper);
            for (Orders orderDetail : drugList) {
                int prodid = orderDetail.getProdid(); // 物品id
                int prodcount = orderDetail.getProdcount(); // 物品数量
                System.out.println("物品id：" + prodid);
                System.out.println("物品数量：" + prodcount);
                // 查询数据库有没有这个药品id
                QueryWrapper<Details> queryWrapper1 = Wrappers.query();
                queryWrapper1.eq("itemid", prodid);
                List<Details> prodidList = DetailsDao.selectList(queryWrapper1);

                if (!prodidList.isEmpty()) {
                    for (Details prodids : prodidList) {
                        int prodidss = prodids.getItemid(); // 物品id
                        int prodcounts =(prodids.getQuantity() + prodcount); // 药品数量
                        String currentTime2 = sdf.format(date);
                        DetailsDao.update(prodidss, prodcounts ,currentTime2); // 修改数量
                    }
                } else {
                    Details details = new Details();
                    details.setItemid(prodid);
                    details.setHouseid(1);
                    details.setQuantity(prodcount);
                    details.setPurchase(currentTime);
                    details.setExpiration(currentTimes);
                    details.setCreatedAt(currentTime);
                    DetailsDao.insert(details);
                }
            }
        }
        return resultBean;
    }
//
//
    //删除
    @RequestMapping("/del/{orderNo}")
    public ResultBean del(@PathVariable("orderNo") String orderNo){

        OrdersDao.deleteByOrderNo(orderNo);
        return new ResultBean();
    }
    @RequestMapping("/del2/{orderNo}")
    public ResultBean del2(@PathVariable("orderNo") String orderNo){

        OrdersDao.deleteByOrderNo2(orderNo);
        return new ResultBean();
    }
//
//
//
//



}
